package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2016102600764590";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDqOahfTz7F2D5c6+0be4gRtGv3C0hrhuc/JZw44p5Rg6pUOsoDhj95M9Xe84OrI4yQ50Dz6xPIYrXrDwZAc3tO9ZlWwNMeVTZTg4eG3uch5tN4axk5mV+1yKR+g4S7yO+6PtKk85h5vuT/s8T8n+RAHC2tIeGJzCDudKaBCpQZu8cf6R1Q4PcyYbUpSGPnX/VQoSbvDj0+V/J7nl1Wv+HOJch65iuLZ4XZa0RmHaYWykB7p1e+5434zv3//wsGea+GaXa0851qzJ9dYvaPkI7ziAfddU+CDzCN58VusTif45cxMaNwxkPrxni60eOI92BhtDUJxdoOleB3cp3jD3G9AgMBAAECggEBAMRG8uCDoL/cWU426mgYWcs0XTEVhGePEeOp2cMVrMP0SNzysuzHKBOiK3v8pn7YeGfJUnXUraxDg9zppzyFSXS+fPPk/EWz9KWBWCXeTuPq8ZU8clQBQeIstCPMhIN0e+ef05clBsYnAPGCLPXh6z0KIACwnraBl6sbl5gVTExOo74z6tvPM8F98hYR0Q9sRSoeOSmLCrCEpBKpE0sHdyHJyBJBaMC66hN+rvDko+78oncuqQ8kEl1Jn1B5JW+3yhfqHXpBVri7ADTwqdb3M7CMTLU/UbxfL4H7YvAsxR8uog+ndrePN7R5SGJRadivCPJFePMse4ngQ0fWBvjqBaUCgYEA97KGjvuyHwy9idJSiyXEk7U+jAzkeJo/k/jMbkRDaW9gbd5V4qnLQGHUFrEbebFPAcgVTuGskMaZLIPnj/xhRkPmcn+oPprChFowTACkLF+zyqffs5y0d6N5fDi0F5Puab7S7lYW+GQV4wnNckJ1cfPUVE9sZHit1vSpQM7SZPMCgYEA8hOHT5iZxkIKPM0fdWDovBG0NgVcSsU9rldG8d+MJVERF9wpYk8CuIJEK7f3PRJvjU/0nO4ePDBSigLJIwAAmgA672qj9LbGovVIwfg5dVwEnmIYmW50xofr9T4Gw0mOA8SLI2XTtXBQGB1sjxlCgVnLvoKaBDt9bOCxpH8uOo8CgYEAq44zki6TpNu/HwjT8Noct60tf/Sh/UHgTQ1iEfEnu4vPfukiFRZC1vmq2niaLgP3U6WP1I2LZs6WYnWNWXWUrgTzRFRUY9/uX5rTnqBHFRDHxtHUp2Mev0ozJy1i1n23//eRwk9QI4O2gQRWDhAnpeAaLLbBvSi0RpA39dIJeK8CgYEApLb2/gj2sTkkYCSugB7VnYpqVcul9dZp1uPaCSAz6vxTJioPGq5qSoOQwTKnDy4pKz05U0KO3iN/NhsZIzCQODlp4JhuQUfQtvcxb5Jn/lwDZIWY+WV4HdJEkpWG4oB9GkE073wM5ssqJI543JGyd/NSuCmDhNe5y0jEdwp2uS8CgYBlpawBrVaZfoREpgfbPQ/fPbJndIbeo1mOmezmRxU5seJpQTLKL8nAqNh+Hm5lBMSVQccY3YnC1VBsINB2XN4gBOH/RJqsicmNzJyzo4AMb+UVA2VPv0dfkFHO8diRGvv3EhZt1FVtbKqjgqDVATMLpHthVgK9AajPPvManpTi8g== ";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw9ZbnYhXW23+ZfaPoDX/eVXbePUHtJvdDdr6X/yGhttvEVMv5WEJzXD/ina4Ys520tXml1oDumHaqkr/rdl5ZNhSjafl7MovFTpUGW7aL0/1TJTkZO4sqa0uxcD2rlpzMXZfBAb5S17th0/YsazTxV4jG3pscPi/MrGd4j41gS89HE6yhWG5LXhHvheh49YHSjuL3DKaE0/HxVssDV6cBEjUYPJ7DpSZxYZxyQShztVq4wZ3IAWOVxgXANd++HLTIEdY0asrsIQErqaj/N00FEJY80ajZcmPFqlPnWRBpu2GGerbIGp9SbA2S3a2y+QA11pI8gfkE/fi0vGkDHB4qQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://cf3t2e.natappfree.cc/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public   String log_path = "D:\\myalipaylog\\";

    private String timeout = "30m";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
